import React, {Component} from 'react';
import {
  Layout,
  Menu,
  Breadcrumb,
  Modal,
  Form,
  Input,
  Select,
  DatePicker,
  Popconfirm,
  Card,
  Row,
  Col,
  Button,
  Radio,
  Dropdown,
  Icon,
  Upload,
  message,
  Table,
  Pagination,
  Checkbox
} from 'antd';
const FormItem = Form.Item;
const {Header, Content, Footer} = Layout;
import _ from 'underscore';
import WrapPage from './_wrap'
import styles from './main.less'
import * as  MessageModel from '../model/message';
import * as  UserModel from '../model/user';

class _Page extends React.Component {
  constructor(props) {
    super(props);
    let user=localStorage.user.cnum;
    this.state = {page:1,pageSize:10,orderBy:'createAt',filter:{},data:{list:[]}};
    this.updateList()
  }
  async updateList(nstate={}){
            nstate = Object.assign({},this.state,nstate) //全备份
            nstate.filter = Object.assign({},this.state.filter,nstate.filter||{})
            let filter = Object.assign(this.state.filter,nstate.filter||{})
            for(let k of Object.keys(nstate.filter)){
              if(filter[k]==''|| filter[k]=='全部'){
                delete nstate.filter[k];
              }
            }
            var res = await  MessageModel.fetch({
              filter:nstate.filter,
              orderBy:nstate.orderBy,
              startPos:nstate.pageSize*(nstate.page-1),
              limit:nstate.pageSize
            })

            let _filter = {cnum:{$in:res.data.list.map(v=>v.user_num)}}
            let arr =[];res.data.list.map(v=>v.to_user.map(d=>arr.push(d.user_num)))
            let _filter2 = {cnum:{$in:arr}}
            console.log('_filter2',_filter2)
            let userRes = await UserModel.fetch({
              filter:_filter,
              startPos:0,
              limit:9999
            })
            let userRes2 = await UserModel.fetch({
              filter:_filter2,
              startPos:0,
              limit:9999
            })
            let userMap = {}; userRes.data.list.map(d=>userMap[d.cnum]=d)
            let userMap2 = {}; userRes2.data.list.map(d=>userMap2[d.cnum]=d)
            res.data.list.map(d=>{
              let user = userMap[d.user_num] || {}
              d.user_name = user.name
              d.to_user.map(v=>{
                let user2 = userMap2[v.user_num] || {}
                v.user_name= user2.name})
            })

            if(res.status == 'success'){
              message.success("更新成功");
              nstate.data = res.data;
              this.setState(nstate)
            }else{
              message.error('更新失败')
            }
          }
          render() {
            const columns = [
            {
              title: '发送消息用户',
              dataIndex: 'user_name',
              key: 'user_name',
            },
            {
              title: '接受消息用户',
              dataIndex: 'to_user',
              key: 'to_user',
              render: (text,record)=> <ul>{Array.from(record.to_user||[]).map(v=><li key={Math.random()}>{v.user_num}</li>)}</ul>,
            },
            {
              title: 'clt_name',
              dataIndex: 'clt_name',
              key: 'clt_name',
              render: text => <a href="">{text}</a>,
            },
            {
              title: '动作',
              dataIndex: 'action',
              key: 'action',
              render: text => <a href="">{text}</a>,
            },
            {
              title: '备注',
              dataIndex: 'note',
              key: 'note',
              render: text => <a href="">{text}</a>,
            }
            ];

            return (
            <WrapPage>
            <div>
            <Row>
            <Col span={12}>消息管理</Col>
            <Col span={12}>
            <Row type="flex" justify="end">
            </Row>
            </Col>
            </Row>
            <Table columns={columns} dataSource={this.state.data.list}
            rowKey={record => record._id} pagination={false} scroll={{ x: 1000}}/>
            <Pagination className="ant-table-pagination"
            total={this.state.data.count} current={this.state.page} pageSize={this.state.pageSize} showSizeChanger
            showTotal={(total, range) => `${range[0]}-${range[1]} of ${total} items`}
            onChange={(page, pageSize)=>{this.updateList({page:page})}}
            onShowSizeChange={(current, size)=>this.updateList({pageSize:size})}/>
            </div>
            </WrapPage>
            );
          }
        }

        export default _Page
